<?php


namespace app\home\controller;

use app\home\model\Renticeliang as RenticeliangModel;
use think\Controller;
use think\Request;

class Renticeliang extends Controller
{
    protected $renticeliangModel;

    public function __construct(
        RenticeliangModel $renticeliangModel
    )
    {
        parent::__construct();
        $this->renticeliangModel = $renticeliangModel;
    }

    public function show(Request $request)
    {
        return $this->fetch();
    }

    public function lists()
    {
        return $this->fetch();
    }

    public function ajax_lists(Request $request)
    {
        $page = $request->param('page', 1);
        $limit = $request->param('limit', 10);
        $where = [];
        $data = $this->renticeliangModel->where($where)->page($page, $limit)->select();
        $count = $this->renticeliangModel->where($where)->count();
        return reorganizeTableData(0, '', $count, $data);
    }

    public function info(Request $request)
    {
        $pid = $request->param('pid', '');
        $res = $this->renticeliangModel->where('pid', $pid)->find();
        return jsonRes($res);
    }

    public function update(Request $request)
    {
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $tizhi = $request->param('tizhi', '');
        $tizhi_lv = $request->param('tizhi_lv', '');
        $feizhi = $request->param('feizhi', '');
        $jirou = $request->param('jirou', '');
        $tuiding = $request->param('tuiding', '');
        $danbaizhi = $request->param('danbaizhi', '');
        $shuifen = $request->param('shuifen', '');
        $shuifen_lv = $request->param('shuifen_lv', '');
        $waiye = $request->param('waiye', '');
        $neiye = $request->param('neiye', '');
        $daixie = $request->param('daixie', '');
        $zhifangdengji = $request->param('zhifangdengji', '');
        $tixing = $request->param('tixing', '');
        $changjiaxinghao = $request->param('changjiaxinghao', '');
        $xiangweijiao = $request->param('xiangweijiao', '');
        $shengao = $request->param('shengao', '');
        $tizhong = $request->param('tizhong', '');
        $bmi = $request->param('bmi', '');
        $amc = $request->param('amc', '');
        $tsf = $request->param('tsf', '');
        $woli = $request->param('woli', '');
        $mamc = $request->param('mamc', '');
        $left_tui = $request->param('left_tui', '');
        $right_tui = $request->param('right_tui', '');

        $data = [
            'pid' => $pid,
            'tizhi' => $tizhi,
            'tizhi_lv' => $tizhi_lv,
            'feizhi' => $feizhi,
            'jirou' => $jirou,
            'tuiding' => $tuiding,
            'danbaizhi' => $danbaizhi,
            'shuifen' => $shuifen,
            'shuifen_lv' => $shuifen_lv,
            'waiye' => $waiye,
            'neiye' => $neiye,
            'daixie' => $daixie,
            'zhifangdengji' => $zhifangdengji,
            'tixing' => $tixing,
            'changjiaxinghao' => $changjiaxinghao,
            'xiangweijiao' => $xiangweijiao,
            'shengao' => $shengao,
            'tizhong' => $tizhong,
            'bmi' => $bmi,
            'amc' => $amc,
            'tsf' => $tsf,
            'woli' => $woli,
            'mamc' => $mamc,
            'left_tui' => $left_tui,
            'right_tui' => $right_tui
        ];

        //判断数据是否存在
        $info = $this->renticeliangModel->where('pid', $pid)->with(['huanzhe' => function ($query) {
            $query->field('id,hospital_id,keshi,name,gender,birthday,created');
        }])->findOrEmpty()->toArray();
        if(empty($info)){
            $res = $this->renticeliangModel->save($data);
        }else{
            $res = $this->renticeliangModel->save($data,['pid' => $info['pid']]);
        }

        if ($res) {
            $resBack = jsonRes($res);
        } else {
            $resBack = jsonRes($res, '更新失败', 99);
        }
        return $resBack;
    }

}